package com.forum.module.blog.service.notification;

import com.alibaba.nacos.shaded.com.google.common.annotations.VisibleForTesting;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.framework.common.exception.util.ServiceExceptionUtils;
import com.forum.framework.common.pojo.PageResult;
import com.forum.framework.common.util.object.BeanUtils;
import com.forum.framework.security.core.util.SecurityUtils;
import com.forum.module.blog.controller.notification.vo.NotificationPageReqVO;
import com.forum.module.blog.controller.notification.vo.NotificationSavaReqVO;
import com.forum.module.blog.dal.dataobject.likes.LikesDO;
import com.forum.module.blog.dal.dataobject.notification.NotificationDO;
import com.forum.module.blog.dal.mysql.notification.NotificationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.forum.module.system.enums.ErrorCodeConstants.LIKES_NOT_EXISTS;
import static com.forum.module.system.enums.ErrorCodeConstants.NOTIFICATION_NOT_EXISTS;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/2 18:34
 */
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, NotificationDO> implements NotificationService {
    private final NotificationMapper notificationMapper;

    public NotificationServiceImpl(NotificationMapper notificationMapper) {
        this.notificationMapper = notificationMapper;
    }

    @Override
    public Long createNotification(NotificationSavaReqVO reqVO) {
        NotificationDO notificationDO = BeanUtils.toBean(reqVO, NotificationDO.class);
        notificationDO.setStatus(0);
        notificationMapper.insert(notificationDO);
        return notificationDO.getId();
    }

    @Override
    public void updateNotification(NotificationSavaReqVO reqVO) {
        NotificationDO updateObj = BeanUtils.toBean(reqVO, NotificationDO.class);
        notificationMapper.updateById(updateObj);
    }

    @Override
    public void deleteById(Long id) {
        // 1. 校验站内信存在
        validateNotificationExists(id);

        notificationMapper.deleteById(id);
    }

    @VisibleForTesting
    NotificationDO validateNotificationExists(Long id) {
        if (id == null) {
            return null;
        }
        NotificationDO notificationDO = notificationMapper.selectById(id);
        if (notificationDO == null) {
            throw ServiceExceptionUtils.exception(NOTIFICATION_NOT_EXISTS);
        }
        return notificationDO;
    }


    @Override
    public PageResult<NotificationDO> getNotificationPage(NotificationPageReqVO pageReqVO) {
        pageReqVO.setUserId(SecurityUtils.getLoginUserId());
        return notificationMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendMessage(Long userId, int type, String message) {
        NotificationDO notificationDO = new NotificationDO();
        notificationDO.setUserId(userId);
        notificationDO.setStatus(0);
        notificationDO.setType(type);
        notificationDO.setContent(message);

        notificationMapper.insert(notificationDO);
    }

    @Override
    public NotificationDO getNotification(Long id) {
        NotificationDO notificationDO = new NotificationDO();
        notificationDO.setId(id);
        notificationDO.setStatus(1);
        notificationMapper.updateById(notificationDO);

        return notificationMapper.selectById(id);
    }
}
